package com.travel.server.manager.impl.command.project.data;

import com.travel.common.excel.ExcelData;
import com.travel.common.exception.WorkCode;
import com.travel.common.exception.WorkException;
import com.travel.common.util.CollectionUtil;
import com.travel.common.util.DateUtil;
import com.travel.common.util.ExcelUtil;
import com.travel.server.manager.dto.request.project.data.ExportProjectApplyRequest;
import com.travel.server.manager.dto.request.project.data.QueryProjectApplyRequest;
import com.travel.server.manager.dto.response.EmptyResponse;
import com.travel.server.manager.dto.response.project.data.QueryProjectApplyResponse.ProjectApplyItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/29 12:14
 * @Desc:
 */
@Component
@Slf4j
public class ExportProjectApplyCommand extends AbstractCommand<ExportProjectApplyRequest, EmptyResponse> {

    @Autowired
    private QueryProjectApplyCommand queryProjectApplyCommand;

    @Override
    protected void checkInput(ExportProjectApplyRequest request) {

    }

    @Override
    public EmptyResponse handle(ExportProjectApplyRequest request) {
        QueryProjectApplyRequest queryProjectApplyRequest = new QueryProjectApplyRequest();
        queryProjectApplyRequest.setStartTime(request.getStartTime());
        queryProjectApplyRequest.setEndTime(request.getEndTime());
        Date startTime = DateUtil.getDataTime(request.getStartTime());
        Date endTime = DateUtil.getDataEndTime(request.getEndTime());
        List<ProjectApplyItem> list = queryProjectApplyCommand.makeProjectApply(startTime, endTime, null, null);
        if(CollectionUtil.isEmpty(list)){
            throw new WorkException(WorkCode.EXCEL_DATA_NULL);
        }
        List<String> title = Arrays.asList("项目", "申请费", "已售订单", "退款订单", "收入总额");
        List<List<Object>> rows = new ArrayList<>();
        for(ProjectApplyItem item : list){
            List<Object> objects = new ArrayList<>();
            objects.add(item.getProject());
            objects.add(item.getApply());
            objects.add(item.getOrder());
            objects.add(item.getRefund());
            objects.add(item.getIncome());
            rows.add(objects);
        }

        ExcelData excelData = new ExcelData();
        excelData.setTitles(title);
        excelData.setRows(rows);
        excelData.setName("项目申请费");
        ExcelUtil.exportExcel(getHttpServletResponse(), "项目申请费", excelData);
        return new EmptyResponse();
    }
}
